package pl.koziolekweb.szkola.KHSerwerSlownik;

// KH-SerwerSlownik/Sesja.java
 
import java.io.*;
import java.net.*;

class Sesja extends Thread {
    Socket        soc =  null;
    BufferedReader br =  null;
    PrintWriter    pw =  null;
    boolean    failed = false;

    Sesja(Socket soc) {
        this.soc = soc;
        try {
            br = new BufferedReader(
                    new InputStreamReader(
                        soc.getInputStream()));
            pw = new PrintWriter(
                    soc.getOutputStream(),true);
        } catch(IOException e) {
            failed = true;
        }
    }

    public void run() {
        String pyt;
        try {
            while((pyt = br.readLine()) != null) {
                String[] arr = pyt.split(" +",3);

                if ("q".equals(arr[0])) return;

                for (int i = 0; i < arr.length; ++i)
                    arr[i] = arr[i].trim().toLowerCase();

                if (       "g".equals(arr[0])) {
                    pw.println(SlownikSerwer.get(arr[1]));
                    if (pw.checkError()) return;

                } else if ("p".equals(arr[0])) {
                    SlownikSerwer.put(arr[1],arr[2]);

                } else {
                    pw.println("Zla skladnia");
                    if (pw.checkError()) return;
                }
            }
        }
        catch(IOException ignore) { }
        finally {
            if (br != null) {
                try {  br.close(); }
                catch(IOException ignore) { }
            }
            if (soc != null) {
                try { soc.close(); }
                catch(IOException igoren) { }
            }
        }
    }
}
